package com.example.drangh;


import android.graphics.ColorSpace;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.List;

import adapter.RlvAdapter;
import bean.HomeBean;
import presenter.HomePresenter;

public class MainActivity extends AppCompatActivity {

    private Toolbar mToobar;
    private RecyclerView mRecycler;
    private LinearLayout mLine;
    private NavigationView mNa;
    private DrawerLayout mDraw;
    private RlvAdapter rlvAdapter;
    private HomePresenter homePresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initPresenter();


    }

    private void initPresenter() {
        homePresenter = new HomePresenter(this);
        homePresenter.getData();
    }

    private void initView() {
        mToobar = (Toolbar) findViewById(R.id.toobar);
        mRecycler = (RecyclerView) findViewById(R.id.recycler);
        mLine = (LinearLayout) findViewById(R.id.line);
        mNa = (NavigationView) findViewById(R.id.na);
        mDraw = (DrawerLayout) findViewById(R.id.draw);
      mToobar.setTitle("toobar");
      setSupportActionBar(mToobar);
        ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, mDraw, mToobar, R.string.app_name, R
                .string.app_name);
          mDraw.addDrawerListener(actionBarDrawerToggle);
           actionBarDrawerToggle.syncState();
           mDraw.addDrawerListener(new DrawerLayout.DrawerListener() {
               @Override
               public void onDrawerSlide(@NonNull View view, float v) {
                   int right = mNa.getRight();

                   mLine.setX(right);
               }

               @Override
               public void onDrawerOpened(@NonNull View view) {

               }

               @Override
               public void onDrawerClosed(@NonNull View view) {

               }

               @Override
               public void onDrawerStateChanged(int i) {

               }
           });
        View headerView = mNa.getHeaderView(0);
        ImageView viewById = headerView.findViewById(R.id.image);

       viewById.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {

           }
       });
       mRecycler.setLayoutManager(new LinearLayoutManager(this));
        rlvAdapter = new RlvAdapter((List<HomeBean.ResultsBean>) this);
       mRecycler.setAdapter(rlvAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(1,1,1,"详细");
        menu.add(1,2,1,"网络");
        menu.add(1,3,1,"瀑布");

        return super.onCreateOptionsMenu(menu);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()){
            case 1:
                mRecycler.setLayoutManager(new LinearLayoutManager(MainActivity.this));
            break;

            case 2:
                mRecycler.setLayoutManager(new GridLayoutManager(MainActivity.this,2));

            break;

            case 3:
                mRecycler.setLayoutManager(new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL));

                rlvAdapter.b=false;
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    public  void getsucces(HomeBean bean){
        List<HomeBean.ResultsBean> results = bean.getResults();

        rlvAdapter.getData(results);
    }

    public void getFile(String msg){
        Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
    }


}


